#ifdef _PLATFORM_WIN32
#include "window_cursor.h"
#include "game_application.h"
#include "script/file_list.h"

#include "util/log.h"				// log
#include "util/common_macros.h"		// macros
#include "util/mmgr.h"

WindowCursor::WindowCursor() :
	m_pCurrent(0)
{
	script::FileList	FL;
	FL.Build("cursors/*.ani");
	for (int i=0, c=FL.GetFileC(); i<c; i++)
	{
		std::string	File = FL.GetFileName(i);
		std::string Name = FL.GetBaseFileName(i);

		CursorData*	pData = new CursorData;
		pData->Cursor = ::LoadCursorFromFile(File.c_str());
		pData->Name	  = Name;
		m_Cursors.AddElement(Name, pData);
		if (!	m_pCurrent)
		{
			m_pCurrent= pData; 
		}
	}
	RefreshCursor();
}

WindowCursor::~WindowCursor()
{
	for (int i = 0; i < (int)m_Cursors.GetElementC(); i++)
	{
		CursorData* pData = m_Cursors.GetElementByIndex(i);
		if (!pData) continue;


		if (HCURSOR	Cursor = pData->Cursor)
		{
			::DestroyCursor(Cursor);
		}
	}
}

WindowCursor& WindowCursor::Instance()
{
	static WindowCursor cur;
	return cur;
}

void WindowCursor::Tick(float dt)
{
	RefreshCursor();
}

void WindowCursor::Render()
{
}

void WindowCursor::SetCursor(const std::string& Name, bool Force)
{
	CursorData*	pData = m_Cursors.GetElement(Name);
	if (pData == m_pCurrent && !Force) return;
	if (!pData) return;

	m_pCurrent = pData;
	RefreshCursor();
}

void WindowCursor::RefreshCursor()
{
	if (m_pCurrent)
	{
		::SetCursor(m_pCurrent->Cursor);
	}
}
#endif
